package com.lib76.socket;

import com.lib76.socket.IMessageInterceptor;
import com.lib76.socket.model.ReceiveEvent;
import com.lib76.socket.model.SocketMessage;

public class MessageInterceptor implements IMessageInterceptor{

	public boolean intercept(ReceiveEvent event) {
		System.out.println(event.getCurrentMessage().getOriginalMessage());
		SocketMessage msg = event.getResponseMessage();
		if(msg != null){
			try {
				Thread.sleep(1000L);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			msg.writeInteger("error", 1);
			msg.writeString("serialNum", event.getCurrentMessage().readString("serialNum"));
			msg.writeFloat("flux", 20f);
			msg.writeString("ci", "1233");
			if(event.getChannel().write(msg)){
				System.out.println(event.getCurrentMessage().readString("command")+"回复成功");
			}else{
				System.out.println(event.getCurrentMessage().readString("command")+"回复失败");
			}
		}
		return true;
	}

}
